package cn.jinjimi.app.main.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.CommonController;
import cn.jinjimi.app.controller.event.TaskListEvent;
import cn.jinjimi.app.entity.TaskListBean;
import cn.jinjimi.app.main.adapter.TaskListAdapter;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 展业列表
 * Created by Jinsen on 2015/6/1.
 */
public class TaskListActivity extends ToolBarCommonActivity {
    XListView mListView = null;

    private View mLoadView = null;
    private TextView mLoadMsgView = null;
    private ImageView mLoadStateIv = null;
    private ProgressBar mLoadingPb = null;

    private TaskListAdapter mAdapter = null;

    private boolean ISREQUESTING = false;

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_show_task_list_layout);
        setTitle(getString(R.string.dis_daily_schedule));
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mListView = (XListView) findViewById(R.id.listview);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        mListView.setTransparentBg();

        mLoadView = findViewById(R.id.activity_list_load_layout);
        mLoadMsgView = (TextView) findViewById(R.id.load_state_msg_textView);
        mLoadStateIv = (ImageView) findViewById(R.id.load_state_fail_tryagain_imageView);
        mLoadingPb = (ProgressBar) findViewById(R.id.load_state_ing_progressBar);
    }

    protected void initListener() {
        mLoadStateIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestData();
            }
        });
    }

    protected void initData() {
        mAdapter = new TaskListAdapter(this);
        mListView.setAdapter(mAdapter);
        requestData();
        loading();
    }

    private void loading() {
        mLoadingPb.setVisibility(View.VISIBLE);
        mLoadStateIv.setVisibility(View.GONE);
        mLoadMsgView.setText(R.string.loading_text);
        mListView.setVisibility(View.GONE);
    }

    private void loadSucc() {
        mLoadView.setVisibility(View.GONE);
        mListView.setVisibility(View.VISIBLE);
    }

    private void loadFail() {
        mLoadingPb.setVisibility(View.GONE);
        mLoadStateIv.setVisibility(View.VISIBLE);
        mLoadMsgView.setText(R.string.loading_fail_for_click);
    }

    private void requestData() {
        if (!ISREQUESTING) {
            CommonController.getTaskList();
            ISREQUESTING = true;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTaskListEvent(TaskListEvent event) {
        ISREQUESTING = false;
        if (event.state == TaskListEvent.SUCC) {
            loadSucc();
            TaskListBean data = event.data;
            mAdapter.setItems(data.getTasks());
        } else if (event.state == TaskListEvent.FAIL) {
            if (mAdapter.getCount() > 0)
                return;
            loadFail();
        }
    }

}
